﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Windows.Forms;
using UserDefineForm.formcomponent;
using UserDefineForm.control.propertymapping;

namespace UserDefineForm.control
{
    public class DesignableNumbericTextBox:DesignableTextBox
    {

        public DesignableNumbericTextBox(DesignPanel designPanel, PropertiesPanelv2 propPnl)
            : base(designPanel, propPnl)
        {
            this.mappingObject = new NumberTextBoxMappingObject();
            txtBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(txtBox_KeyPress);
        }

        void txtBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            NumberFormatInfo numberFormatInfo = System.Globalization.CultureInfo.CurrentCulture.NumberFormat;
            string decimalSeparator = numberFormatInfo.NumberDecimalSeparator;
            string negativeSign = numberFormatInfo.NegativeSign;

            string keyInput = e.KeyChar.ToString();

            if (Char.IsDigit(e.KeyChar))
            {
                // Digits are OK
            }
            else if (keyInput.Equals(decimalSeparator) )
            {
                if (txtBox.Text.IndexOf(decimalSeparator) > 0)
                {
                    e.Handled = true;
                }
            }
            else if (keyInput.Equals(negativeSign))
            {
                if (txtBox.Text.Length > 0)
                {
                    e.Handled = true;
                }
            }
             
            else if (e.KeyChar == '\b')
            {
                // Backspace key is OK
            }
            //    else if ((ModifierKeys & (Keys.Control | Keys.Alt)) != 0)
            //    {
            //     // Let the edit control handle control and alt key combinations
            //    }
            else
            {
                // Consume this invalid key and beep
                e.Handled = true;
                //    MessageBeep();
            }

        }

        public override ControlType getType()
        {
            return ControlType.NumberTextBox;
        }
    }
}
